package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.entity.realTime.EntityUserSevenLeftByDay;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 实时数据-七留manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class UserSevenLeftByDayManager extends ManagerQueryCacheBase<EntityUserSevenLeftByDay> {
	private static final long serialVersionUID = 1L;
	
	// 得到符合平台、渠道、区服条件的查看日次留总和
	public String getAllUserSecondLeftByConditions(Date lookDate, String queryPlatform, String queryChannel, String queryServer) {
		List<Object[]> oo = findByQuery("select day,sum(usercreatenum),sum(loginusernum) from EntityUserSecondLeftByDay where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid ", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime() - Time.DAY)), "dayEnd", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
		return getResult(oo);
	}

    // 得到符合平台、渠道、区服条件的查看日七留总和
    public String getAllUserSevenLeftByConditions(Date lookDate, String queryPlatform, String queryChannel, String queryServer) {
        List<Object[]> oo = findByQuery("select day,sum(usercreatenum),sum(loginusernum) from EntityUserSevenLeftByDay where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid ", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime() - 6 * Time.DAY)), "dayEnd", new Date(Time.getBeginningOfDay(lookDate.getTime() - 5 * Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
        return getResult(oo);
    }

    // 得到符合平台、渠道、区服条件的查看日七留总和
    public String getAllMacSevenLeftByConditions(Date lookDate, String queryPlatform, String queryChannel, String queryServer) {
        List<Object[]> oo = findByQuery("select day,sum(maccreatenum),sum(loginmacnum) from EntityMacSevenLeftByDay where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid ", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime() - 6 * Time.DAY)), "dayEnd", new Date(Time.getBeginningOfDay(lookDate.getTime() - 5 * Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
        return getResult(oo);
    }

	
	// 根据查询结果，拼装最终结果
	private String getResult(List<Object[]> oo) {
		String result = null;
		
		if (oo != null && oo.size() > 0 && oo.get(0)[0] != null) {
			int usercreatenum = 0;
			int loginusernum = 0;
			for(Object[] o : oo) {
				usercreatenum += Integer.parseInt(o[1].toString());
				loginusernum += Integer.parseInt(o[2].toString());
			}
			
			result = PageHelper.double2Percent(loginusernum * 1.0 / (usercreatenum == 0 ? 1 : usercreatenum));
		}
		
		return result;
	}
	
	public List<Object[]> getPaltform7LeftRealTimeDatas(Date lookDate) {
		return findByQuery("select platformid,user_seven_left from EntityUserSevenLeftByDay where day=:dayStart and channelid = :channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "channelid", "0", "serverid", "0");
	}
	
	public List<Object[]> getPaltform7LeftRealTimeMacDatas(Date lookDate) {
		return findByQuery("select platformid,mac_seven_left from EntityMacSevenLeftByDay where day=:dayStart and channelid = :channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "channelid", "0", "serverid", "0");
	}
	
	public List<Object[]> getChannel7LeftRealTimeDatas(Date lookDate) {
		return findByQuery("select channelid,user_seven_left from EntityUserSevenLeftByDay where day=:dayStart and platformid=:platformid and channelid != :channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0");
	}
	
	public List<Object[]> getChannel7LeftRealTimeMacDatas(Date lookDate) {
		return findByQuery("select channelid,mac_seven_left from EntityMacSevenLeftByDay where day=:dayStart and platformid=:platformid and channelid != :channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0");
	}
}
